package com.zw.pdm.module.craft.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.craft.CraMakeDetail;
import com.zw.pdm.entity.craft.CraMakeSheet;
import com.zw.pdm.entity.est.Card;
import com.zw.pdm.entity.production.DrawChange;
import com.zw.pdm.entity.production.MakeChange;
import com.zw.pdm.entity.production.MakePlan;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.craft.model.ServiceTaskModel;
import com.zw.pdm.module.craft.service.CraftMaintainService;
import com.zw.pdm.module.craft.service.CraftReceiveService;
import com.zw.pdm.module.craft.service.CraftReleaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @Description: 计划查询接口
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/craft/maintain")
@SuppressWarnings("all")
public class CraftMaintainController extends BaseController {

    @Autowired
    private CraftReceiveService craftReceiveService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private CraftReleaseService craftReleaseService;
    @Autowired
    private CraftMaintainService craftMaintainService;

    @GetMapping
    @RequiresPermissions({"tec:proDirectory:view"})
    public String page(Model model) {
        model.addAttribute("specialId", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("deptName", craftReleaseService.findCraftSectionByDeptId(4));
        return "craft/maintain/page";
    }

    /**
     * 列表
     */
    @ResponseBody
    @PostMapping("/list")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult list(CraMakeSheet craMakeSheet) {
        startPage();
        List<CraMakeSheet> list = craftMaintainService.selectCraftMakeSheet(craMakeSheet);
        return jsonResult(list);
    }


    /**
     * 查看操作
     */
    @GetMapping("/info/{sheetId}")
//    @RequiresPermissions({"report:query:view"})
    public String info(@PathVariable("sheetId") Integer sheetId, Model model) {
        CraMakeSheet craMakeSheet = craftMaintainService.selectCraftMakeSheetModel(sheetId);

        model.addAttribute("craMakeSheet", craMakeSheet);
        return "craft/maintain/info";
    }

    /**
     * 列表明细
     */
    @ResponseBody
    @PostMapping("/detail/list")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult detailList(Integer sheetId, String sheetType) {
        startPage();
        List<CraMakeDetail> craMakeDetailList = craftMaintainService.selectCraftMakeDetail(sheetId);
        return jsonResult(craMakeDetailList);
    }


    /**
     * 拆分保存
     *
     * @param craMakeDetail
     * @return
     */
    @ResponseBody
    @PostMapping("/detail/maintain")
    @RequiresPermissions({"tec:proDirediting:edit"})
    public JsonResult maintainCraMakeDetail(CraMakeDetail craMakeDetail) {
        CraMakeDetail craMakeDetailOld = craftMaintainService.selectCraftMakeDetailOld(craMakeDetail.getId());

        return jsonResult(craftMaintainService.insertMaintainDetail(craMakeDetailOld, craMakeDetail));
    }
    /**
     * 拆分修改保存
     *
     * @param craMakeDetail
     * @return
     */
    @ResponseBody
    @PostMapping("/detail/editMaintain")
    @RequiresPermissions({"tec:proDirediting:edit"})
    public JsonResult editMaintain(CraMakeDetail craMakeDetail) {
        CraMakeDetail craMakeDetailOld = craftMaintainService.selectCraftMakeDetailOld(craMakeDetail.getId());

        return jsonResult(craftMaintainService.editMaintainDetail(craMakeDetailOld, craMakeDetail));
    }

    /**
     * 删除拆分
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/detail/del")
    @RequiresPermissions({"tec:proDirediting:del"})
    public JsonResult maintainCraMakeDetail(Integer id) {
        return jsonResult(craftMaintainService.delCraMakeDetailRelease(id));
    }
    /**
     * 整体外协
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/detail/outsourcing")
//    @RequiresPermissions({"tec:proDirediting:del"})
    public JsonResult outsourcing(Integer id) {
        return jsonResult(craftMaintainService.outsourcingCraMakeDetailRelease(id,0,1,1));
    }
    /**
     * 取消整体外协
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/detail/reOutsourcing")
//    @RequiresPermissions({"tec:proDirediting:del"})
    public JsonResult reOutsourcing(Integer id) {
        return jsonResult(craftMaintainService.outsourcingCraMakeDetailRelease(id,1,0,0));
    }
}
